package com.yearEndWorkload.listen;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.yearEndWorkload.pojo.entity.PerformanceAppraisal;
import com.yearEndWorkload.pojo.excel.importTemplate.PerformanceAppraisalExcel;
import com.yearEndWorkload.service.PerformanceAppraisalService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.RecordFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Data
@Component
@Slf4j
public class PerformanceAppraisalListener implements ReadListener<PerformanceAppraisalExcel> {
    @Autowired
    PerformanceAppraisalService performanceAppraisalService;

    private final List<PerformanceAppraisal> performanceAppraisalList =new ArrayList<>();
    private final List<PerformanceAppraisal> addList = new ArrayList<>();
    private final List<PerformanceAppraisal> updateList = new ArrayList<>();

    private final Set<PerformanceAppraisalExcel> performanceAppraisalExcelSet =new HashSet<>();
    private Integer record;
        @Override
        public void invoke(PerformanceAppraisalExcel performanceAppraisalExcel, AnalysisContext analysisContext) {
            try {
                if (!performanceAppraisalExcelSet.contains(performanceAppraisalExcel)) {
                    PerformanceAppraisal performanceAppraisal = new PerformanceAppraisal();
                    performanceAppraisal.setId(null);
                    performanceAppraisal.setYear(performanceAppraisalExcel.getYear());
                    performanceAppraisal.setTeacherName(performanceAppraisalExcel.getTeacherName());
                    performanceAppraisal.setAssessMonths(performanceAppraisalExcel.getAssessMonths());
                    performanceAppraisal.setAmount(performanceAppraisalExcel.getFormattedAmount());
                    performanceAppraisal.setReporter(performanceAppraisalExcel.getReporter());
                    performanceAppraisalList.add(performanceAppraisal);
                    performanceAppraisalExcelSet.add(performanceAppraisalExcel);
                }
            }catch (RecordFormatException e) {
                log.warn("发现重复记录: {}", performanceAppraisalExcel, e);            }
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            List<PerformanceAppraisal> performanceAppraisals = performanceAppraisalService.list();
            performanceAppraisalList.forEach(performanceAppraisal -> {
                Optional<PerformanceAppraisal> dbPerformanceAppraisalOptional = performanceAppraisals.stream()
                        .peek(dbPerformanceAppraisal -> dbPerformanceAppraisal.setId(null))
                        .filter(dbPerformanceAppraisal -> dbPerformanceAppraisal.equals(performanceAppraisal))
                        .findFirst();
                dbPerformanceAppraisalOptional.ifPresent(dbPerformanceAppraisal -> log.info("记录已存在数据库:{}",dbPerformanceAppraisal));
                if(dbPerformanceAppraisalOptional.isEmpty()){
                    addList.add(performanceAppraisal);
                }
            });
            List<PerformanceAppraisal> mergedList = Stream.concat(updateList.stream(),addList.stream()).collect(Collectors.toList());
            performanceAppraisalService.saveOrUpdateBatch(mergedList);
            setRecord(mergedList.size());
            initList();
    }
    public void initList(){
        performanceAppraisalList.clear();
        updateList.clear();
        addList.clear();
        performanceAppraisalExcelSet.clear();
    }
}
